package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.mscyb.bean.*;
import com.jy.adscm.operation.mscyb.mapper.mysql.*;
import com.jy.adscm.operation.mscyb.service.JqbzPartsassemblyService;
import com.jy.adscm.operation.purchase.bean.Purchasebom;
import com.jy.adscm.operation.purchase.mapper.mysql.PurchasebomMapper;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingType;
import com.jy.adscm.zltx.mapper.mysql.JyDrawingTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-11-26
 */
@Service
public class JqbzPartsassemblyServiceImpl extends ServiceImpl<JqbzPartsassemblyMapper, JqbzPartsassembly> implements JqbzPartsassemblyService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyCodeMapper jyCodeMapper;
    @Autowired
    JqbzSettingsMapper  jqbzSettingsMapper;
    @Autowired
    JqbzSettingsInputMapper jqbzSettingsInputMapper;
    @Autowired
    JqbzMaterialMapper jqbzMaterialMapper;
    @Autowired
    JqbzSettingProecssMapper jqbzSettingProecssMapper;
    @Autowired
    JqbzAssemblyMapper jqbzAssemblyMapper;
     @Autowired
     CgMaterialMapper cgMaterialMapper;
     @Autowired
    JyDrawingTypeMapper jyDrawingTypeMapper;
    @Override
    public Boolean updateJqbzPartsassembly(JqbzPartsassembly jqbzPartsassembly, Token token) throws AdscmSystemException {
        //待办修改
        if (jqbzPartsassembly.getTaskstate()==0 && jqbzPartsassembly.getTaskfinishstate()==0 &&
                "0".equals(jqbzPartsassembly.getIstaskstart()) &&  "0".equals(jqbzPartsassembly.getIstaskbegin()) &&
                "0".equals(jqbzPartsassembly.getIstaskend())  && "0".equals(jqbzPartsassembly.getIsreviewtask())
        ){

            return  super.updateById(jqbzPartsassembly);
        }
        //接收任务
        if (jqbzPartsassembly.getTaskstate()==0 && jqbzPartsassembly.getTaskfinishstate()==0 &&
                "1".equals(jqbzPartsassembly.getIstaskstart()) &&  "0".equals(jqbzPartsassembly.getIstaskbegin()) &&
                "0".equals(jqbzPartsassembly.getIstaskend())  && "0".equals(jqbzPartsassembly.getIsreviewtask())
        ){
            jqbzPartsassembly.setIstaskstart("1");
            jqbzPartsassembly.setTaskstartaddtime(new Date());
            jqbzPartsassembly.setTaskstate(1);
            return  super.updateById(jqbzPartsassembly);
        }
        //开始任务
        if (jqbzPartsassembly.getTaskstate()==1 && jqbzPartsassembly.getTaskfinishstate()==0 &&
                "1".equals(jqbzPartsassembly.getIstaskstart()) &&  "1".equals(jqbzPartsassembly.getIstaskbegin()) &&
                "0".equals(jqbzPartsassembly.getIstaskend())  && "0".equals(jqbzPartsassembly.getIsreviewtask())
        ){
            jqbzPartsassembly.setIstaskbegin("1");
            jqbzPartsassembly.setTaskbeginaddtime(new Date());
            return super.updateById(jqbzPartsassembly);
        }
        //结束任务
        if (jqbzPartsassembly.getTaskstate()==1 && jqbzPartsassembly.getTaskfinishstate()==0 &&
                "1".equals(jqbzPartsassembly.getIstaskstart()) &&  "1".equals(jqbzPartsassembly.getIstaskbegin()) &&
                "1".equals(jqbzPartsassembly.getIstaskend())  && "0".equals(jqbzPartsassembly.getIsreviewtask())
        ){
            jqbzPartsassembly.setIstaskend("1");
            jqbzPartsassembly.setTaskendaddtime(new Date());
            jqbzPartsassembly.setTaskfinishstate(1);
            //查看是否有对应的条码和批号生成如果没得就生成
            if (!AdscmBaseUtils.getInstance().isNotEmpty(jqbzPartsassembly.getTaskcode(),null)){
                //拿到code生成最大条码
                JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(jqbzPartsassembly.getResourcename());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzSettings.getCode(),null)){
                    String yearMonth = new SimpleDateFormat("yyMMdd").format(new Date());
                    jqbzPartsassembly.setBatchnums(yearMonth);
                    String code=jqbzSettings.getCode()+yearMonth;
                    Wrapper<JqbzPartsassembly> getMaxCodeWrapper =new EntityWrapper<>();
                    getMaxCodeWrapper.like("TASKCODE",code);
                    getMaxCodeWrapper.orderBy("TASKCODE",false);
                    getMaxCodeWrapper.last("limit 1");
                    List<JqbzPartsassembly> jqbzPartsassemblies = super.selectList(getMaxCodeWrapper);
                    if (jqbzPartsassemblies!=null && jqbzPartsassemblies.size()>0){
                        JqbzPartsassembly jqbzPartsassembly1 = jqbzPartsassemblies.get(0);
                        code=jqbzPartsassembly1.getTaskcode().replace(jqbzSettings.getCode(),"");
                        long temp=Long.valueOf(code)+1;
                        jqbzPartsassembly.setTaskcode(jqbzSettings.getCode()+String.valueOf(temp));
                    }else {
                        jqbzPartsassembly.setTaskcode(jqbzSettings.getCode()+yearMonth+"001");
                    }
                    jqbzPartsassembly.setBatchnums(yearMonth);
                }
            }


            return super.updateById(jqbzPartsassembly);
        }
        //复核任务
        if (jqbzPartsassembly.getTaskstate()==1 && jqbzPartsassembly.getTaskfinishstate()==1 &&
                "1".equals(jqbzPartsassembly.getIstaskstart()) &&  "1".equals(jqbzPartsassembly.getIstaskbegin()) &&
                "1".equals(jqbzPartsassembly.getIstaskend())  && "1".equals(jqbzPartsassembly.getIsreviewtask())
        ){
            jqbzPartsassembly.setIsreviewtask("0");
            jqbzPartsassembly.setReviewaddtime(new Date());
            jqbzPartsassembly.setReviewperson(token.getUser().getId().toString());
            return super.updateById(jqbzPartsassembly);
        }

        return null;
    }

    @Override
    public Page<JqbzPartsassembly> selectJqbzPartsassembly(Page<JqbzPartsassembly> page , QueryJqbzPartsassembly queryJqbzPartsassembly,Token token) throws AdscmSystemException {
        Wrapper<JqbzPartsassembly> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKSTATE",queryJqbzPartsassembly.getTaskState());
        wrapper.eq("TASKFINISHSTATE",queryJqbzPartsassembly.getTaskFinishState());
        wrapper.eq("isReviewTask",queryJqbzPartsassembly.getIsReviewTask());
        //判断是生产一部工作人员  215
        Boolean aBoolean = jyUserMapper.queryUserIsHasRole(token.getUser().getId().toString(), "215");
        if (aBoolean){
            wrapper.eq("TASKPERFORMPERSON",token.getUser().getId().toString());
        }

        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqbzPartsassembly.getCode(),null)){
             wrapper.like("TASKCODE",queryJqbzPartsassembly.getCode());
         }
         if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqbzPartsassembly.getName(),null)){
             wrapper.eq("RESOURCENAME",queryJqbzPartsassembly.getName());
         }
       page = super.selectPage(page,wrapper);
        List<JqbzPartsassembly> jqbzPartsassemblies = page.getRecords();

        for (JqbzPartsassembly jqbzPartsassembly : jqbzPartsassemblies) {
            if ( jqbzPartsassembly.getTaskreleaseperson()!=null && !"".equals( jqbzPartsassembly.getTaskreleaseperson())){
                JyUser jyUser = jyUserMapper.selectById(jqbzPartsassembly.getTaskreleaseperson());
                jqbzPartsassembly.setTaskreleasepersonName(jyUser.getName());
            }
            if ( jqbzPartsassembly.getTaskperformperson()!=null && !"".equals( jqbzPartsassembly.getTaskperformperson())){
                JyUser jyUser = jyUserMapper.selectById(jqbzPartsassembly.getTaskperformperson());
                jqbzPartsassembly.setTaskperformpersonName(jyUser.getName());
            }

            if ( jqbzPartsassembly.getReviewperson()!=null && !"".equals( jqbzPartsassembly.getReviewperson())){
                JyUser jyUser = jyUserMapper.selectById(jqbzPartsassembly.getReviewperson());
                jqbzPartsassembly.setReviewpersonName(jyUser.getName());
            }
            if ( jqbzPartsassembly.getTasknums()!=null && !"".equals( jqbzPartsassembly.getTasknums())){
                JyUser jyUser = jyUserMapper.selectById(jqbzPartsassembly.getTasknums());
                jqbzPartsassembly.setTasknumsName(jyUser.getName());
            }
             if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzPartsassembly.getResourcename(),null)){
                 JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById( Integer.valueOf(jqbzPartsassembly.getResourcename()));
                 if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzSettings,null)){
                     jqbzPartsassembly.setResourcenameName(jqbzSettings.getName());
                 }else {
                     jqbzPartsassembly.setResourcenameName(jqbzPartsassembly.getResourcename());
                 }

             }
             //查询机器类型
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzPartsassembly.getMacType(),null)){
                JyDrawingType jyDrawingType = jyDrawingTypeMapper.selectById(jqbzPartsassembly.getMacType());
                jqbzPartsassembly.setMacTypeName(jyDrawingType.getType());
            }

        }
        page.setTotal(super.selectCount(wrapper));

        return page;
    }

    @Override
    public List<JyUser> selectTaskPerformPerson() throws AdscmSystemException {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("94");
        return jyUsers;
    }
     @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertJqbzPartsassembly(JqbzPartsassembly jqbzPartsassembly) throws AdscmSystemException {
        boolean result=false;
        if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzPartsassembly.getResourcename(),null)){
            JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(jqbzPartsassembly.getResourcename());

            jqbzPartsassembly.setMacType(Integer.valueOf(jqbzSettings.getType()));
            jqbzPartsassembly.setState(null);
            result=  super.insert(jqbzPartsassembly);
            //生成部装物料输入列表
            Wrapper<JqbzSettingsInput> wrapper=new EntityWrapper<>();
            wrapper.eq("pid",jqbzSettings.getId());
            List<JqbzSettingsInput> jqbzSettingsInputs = jqbzSettingsInputMapper.selectList(wrapper);
            for (JqbzSettingsInput jqbzSettingsInput : jqbzSettingsInputs) {
                JqbzMaterial jqbzMaterial=new JqbzMaterial();
                CgMaterial cgMaterial = cgMaterialMapper.selectById(jqbzSettingsInput.getCid());
                jqbzMaterial.setTid(jqbzPartsassembly.getTid());
                jqbzMaterial.setName(jqbzSettingsInput.getCid()+"");
                jqbzMaterial.setCode(cgMaterial.getCode());
                jqbzMaterial.setType(cgMaterial.getType());
                jqbzMaterial.setIsAssembly(0);
                jqbzMaterial.setNum(jqbzSettingsInput.getNum());
                jqbzMaterialMapper.insert(jqbzMaterial);
            }
            //生成装配过程记录
            Wrapper<JqbzSettingProecss> jqbzSettingProecssWrapper=new EntityWrapper<>();
            jqbzSettingProecssWrapper.eq("pid",jqbzSettings.getId());
            jqbzSettingProecssWrapper.eq("isEnable","1");
            List<JqbzSettingProecss> jqbzSettingProecsses = jqbzSettingProecssMapper.selectList(jqbzSettingProecssWrapper);
            for (JqbzSettingProecss jqbzSettingProecss : jqbzSettingProecsses) {
                JqbzAssembly jqbzAssembly =new JqbzAssembly();
                jqbzAssembly.setPid(jqbzPartsassembly.getTid());
                jqbzAssembly.setCid(jqbzSettingProecss.getId());
                jqbzAssembly.setName(jqbzSettingProecss.getName());
                jqbzAssembly.setReference(jqbzSettingProecss.getReference());
                jqbzAssembly.setUnit(jqbzSettingProecss.getUnit());
                jqbzAssembly.setType(jqbzSettingProecss.getType());
                jqbzAssembly.setRank(jqbzSettingProecss.getRank());
                jqbzAssembly.setRemark(jqbzSettingProecss.getRemark());
                jqbzAssemblyMapper.insert(jqbzAssembly);
            }

        }

        return result;
    }
    @Transactional
    @Override
    public JqbzPartsassembly updateJqbzPartsassemblyValue(String id, Integer num) throws AdscmSystemException{
        synchronized (this){
         //查询到该条部装记录
            JqbzPartsassembly jqbzPartsassembly = super.selectById(id);
            if (num==-1){
                if (jqbzPartsassembly.getIsoutstorage()==1){
                    throw  new AdscmSystemException("物料已经出库，请刷新页面选择其他物料");
                }
                if (jqbzPartsassembly.getState()!=1){
                    throw  new AdscmSystemException("物料非合格状态，不能出库");
                }
                jqbzPartsassembly.setIsoutstorage(1);
                jqbzPartsassembly.setIsreviewtask("1");
            }else if (num==1){
                jqbzPartsassembly.setIsoutstorage(0);
                jqbzPartsassembly.setIsreviewtask("0");
            }
            super.updateById(jqbzPartsassembly);

            return jqbzPartsassembly;
        }

    }

    @Override
    public Page<JqbzPartsassembly> selectUnqualifiedParts(Page<JqbzPartsassembly>page , QueryJqbzPartsassembly queryJqbzPartsassembly) {
        Wrapper<JqbzPartsassembly> wrapper =new EntityWrapper<>();
        wrapper.eq("state",0);
        wrapper.eq("ISTASKBEGIN",1);
        wrapper.eq("ISTASKEND",1);
        wrapper.orNew();
        wrapper.eq("state",0);
        wrapper.isNotNull("repair");
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqbzPartsassembly.getCode(),null)){
            wrapper.like("TASKCODE",queryJqbzPartsassembly.getCode());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqbzPartsassembly.getName(),null)){
            wrapper.eq("RESOURCENAME",queryJqbzPartsassembly.getName());
        }

        page = super.selectPage( page,wrapper);
        List<JqbzPartsassembly>  jqbzPartsassemblies= page.getRecords();
        for (JqbzPartsassembly jqbzPartsassembly : jqbzPartsassemblies) {
            JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(jqbzPartsassembly.getResourcename());
            jqbzPartsassembly.setResourcenameName(jqbzSettings.getName());
        }
        page.setTotal(super.selectCount(wrapper));
        return page;
    }

    @Override
    public boolean deleteByIdJqbzPartsassembly(Integer id)throws AdscmSystemException {
        //
        JqbzPartsassembly jqbzPartsassembly = super.selectById(id);
        if (jqbzPartsassembly.getTaskstate()!=0){
         throw new AdscmSystemException("非待办任务不能删除");
        }
       Wrapper<JqbzMaterial> jqbzMaterialWrapper =new EntityWrapper<>();
        jqbzMaterialWrapper.eq("tid",id);
         jqbzMaterialMapper.delete(jqbzMaterialWrapper);
         super.deleteById(id);
         Wrapper<JqbzAssembly> jqbzAssemblyWrapper=new EntityWrapper<>();
        jqbzAssemblyWrapper.eq("pid" ,id);
         jqbzAssemblyMapper.delete(jqbzAssemblyWrapper);
        return false;
    }
}
